/*
 * The Open Source Unix Installer
 *
 * The MIT License
 *
 * Copyright 2011-2012 Andrey Pudov.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * @author  Andrey Pudov    <andrey@andreypudov.com>
 * @version 0.00.00
 * @name    u_xml_document.h
 * @date:   Mar 15, 2012
 */

#ifndef INSTALLER_UTIL_XML_U_DOCUMENT_H_
#define INSTALLER_UTIL_XML_U_DOCUMENT_H_

#include <stdio.h>
#include <stdbool.h>

#include "xml_document.h"

#include "u_main.h"

#ifdef __cplusplus
extern "C" {
#endif

bool u_installer_xml_document_start();          /* unit entry point */

bool u_installer_xml_document_new();
bool u_installer_xml_document_delete();
bool u_installer_xml_document_add_element();
bool u_installer_xml_document_get_element();

bool u_installer_xml_document_start() {
    return (u_installer_xml_document_new()
        && u_installer_xml_document_delete()
    	&& u_installer_xml_document_add_element()
    	&& u_installer_xml_document_get_element());
}

bool u_installer_xml_document_new() {
    XMLDocument *document = installer_xml_document_new();

    if (document == NULL) {
        u_installer_main_print("xml_document", "new()", false);

        return false;
    }
    
    u_installer_main_print("xml_document", "new()", true);

    installer_xml_document_delete(document);

    return true;
}

bool u_installer_xml_document_delete() {
    /* unit case is a part of new() */        
    u_installer_main_print("xml_document", "delete()", true);

    return true;
}

bool u_installer_xml_document_add_element() {
    XMLDocument *document = installer_xml_document_new();
    
    XMLElement *element1 = installer_xml_element_new(L"elem1", L"val1");
    XMLElement *element2 = installer_xml_element_new(L"elem2", L"val2");
    XMLElement *element3 = installer_xml_element_new(L"elem3", L"val3");
    
    if ((document == NULL) || (element1 == NULL) || (element2 == NULL) 
	    || (element3 == NULL)) {
        u_installer_main_print("xml_document", "add_element(1)", false);

        return false;
    }
    
    /* document shall have no elements by default */
    if (document->elements != NULL) {
        u_installer_main_print("xml_document", "add_element(2)", false);
	
	    return false;
    }
    
    installer_xml_document_add_element(document, element1);
    if ((wcscmp(installer_xml_element_get_name(document->elements), L"elem1") != 0)
        || (wcscmp(installer_xml_element_get_value(document->elements), L"val1") != 0)) {
		
	    u_installer_main_print("xml_document", "add_element(3)", false);
	
	    return false;
    }
    
    /* add same element to the list */
    installer_xml_document_add_element(document, element1);
    if ((wcscmp(installer_xml_element_get_name(document->elements), L"elem1") != 0)
	        || (wcscmp(installer_xml_element_get_value(document->elements), L"val1") != 0)
	        || (wcscmp(installer_xml_element_get_name(document->elements->next), L"elem1") != 0)
	        || (wcscmp(installer_xml_element_get_value(document->elements->next), L"val1") != 0)) {
	        
	    u_installer_main_print("xml_document", "add_element(4)", false);
	
	    return false;
    }

    installer_xml_document_add_element(document, element2);
    if ((wcscmp(installer_xml_element_get_name(document->elements), L"elem1") != 0)
	        || (wcscmp(installer_xml_element_get_value(document->elements), L"val1") != 0)
	        || (wcscmp(installer_xml_element_get_name(document->elements->next), L"elem1") != 0)
	        || (wcscmp(installer_xml_element_get_value(document->elements->next), L"val1") != 0)
	        || (wcscmp(installer_xml_element_get_name(document->elements->next->next), L"elem2") != 0)
	        || (wcscmp(installer_xml_element_get_value(document->elements->next->next), L"val2") != 0)) {
		
	    u_installer_main_print("xml_document", "add_element(5)", false);
	
	    return false;
    }
    
    installer_xml_document_add_element(document, element3);
    if ((wcscmp(installer_xml_element_get_name(document->elements), L"elem1") != 0)
	        || (wcscmp(installer_xml_element_get_value(document->elements), L"val1") != 0)
	        || (wcscmp(installer_xml_element_get_name(document->elements->next), L"elem1") != 0)
	        || (wcscmp(installer_xml_element_get_value(document->elements->next), L"val1") != 0)
	        || (wcscmp(installer_xml_element_get_name(document->elements->next->next), L"elem2") != 0)
	        || (wcscmp(installer_xml_element_get_value(document->elements->next->next), L"val2") != 0)
	        || (wcscmp(installer_xml_element_get_name(document->elements->next->next->next), L"elem3") != 0)
	        || (wcscmp(installer_xml_element_get_value(document->elements->next->next->next), L"val3") != 0)) {
		
	    u_installer_main_print("xml_document", "add_element(6)", false);
	
	    return false;
    }
            
    u_installer_main_print("xml_document", "add_element()", true);
    
    return true;
}

bool u_installer_xml_document_get_element() {
    XMLDocument *document = installer_xml_document_new();
    
    /*
     * element2 have key same as attribute1
     * element3 have value same as element2
     */
    XMLElement *element1 = installer_xml_element_new(L"elem1", L"val1");
    XMLElement *element2 = installer_xml_element_new(L"elem1", L"val2");
    XMLElement *element3 = installer_xml_element_new(L"elem3", L"val2");
    
    XMLElement *unit = NULL;
    
    if ((document == NULL) || (element1 == NULL) || (element2 == NULL) 
	    || (element3 == NULL)) {
        u_installer_main_print("xml_document", "get_element(1)", false);

        return false;
    }
        
    installer_xml_document_add_element(document, element1);
    installer_xml_document_add_element(document, element2);
    installer_xml_document_add_element(document, element2);
    installer_xml_document_add_element(document, element3);
    
    unit = installer_xml_document_get_element(document, NULL);
    if ((wcscmp(installer_xml_element_get_name(unit), L"elem1") != 0)
        || (wcscmp(installer_xml_element_get_value(unit), L"val1") != 0)) {
		
	    u_installer_main_print("xml_document", "get_element(2)", false);
	
	    return false;
    }
    
    unit = installer_xml_document_get_element(document, unit);
    if ((wcscmp(installer_xml_element_get_name(unit), L"elem1") != 0)
        || (wcscmp(installer_xml_element_get_value(unit), L"val2") != 0)) {
		
	    u_installer_main_print("xml_document", "get_element(3)", false);
	
	    return false;
    }
    
    unit = installer_xml_document_get_element(document, unit);
    if ((wcscmp(installer_xml_element_get_name(unit), L"elem1") != 0)
        || (wcscmp(installer_xml_element_get_value(unit), L"val2") != 0)) {
		
	    u_installer_main_print("xml_document", "get_element(4)", false);
	
	    return false;
    }
    
    unit = installer_xml_document_get_element(document, unit);
    if ((wcscmp(installer_xml_element_get_name(unit), L"elem3") != 0)
        || (wcscmp(installer_xml_element_get_value(unit), L"val2") != 0)) {
		
	    u_installer_main_print("xml_document", "get_element(5)", false);
	
	    return false;
    }
    
    unit = installer_xml_document_get_element(document, unit);
    if (unit != NULL) {
        u_installer_main_print("xml_document", "get_element(6)", false);
	
	    return false;
    }
    
    u_installer_main_print("xml_document", "get_element()", true);
    
    return true;
}

#ifdef __cplusplus
}
#endif

#endif /* INSTALLER_UTIL_XML_U_DOCUMENT_H_ */ 
